package com.xjrsoft.module.customerTwo.companyManage.calculationExampleForm.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.companyManage.calculationExampleForm.entity.Table01474;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import java.util.ArrayList;
import com.xjrsoft.module.customerTwo.companyManage.calculationExampleForm.entity.Table18607;
import com.xjrsoft.module.customerTwo.companyManage.calculationExampleForm.service.ITable18607Service;
import com.xjrsoft.module.customerTwo.companyManage.calculationExampleForm.dto.Table01474ListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customerTwo.companyManage.calculationExampleForm.mapper.Table01474Mapper;
import com.xjrsoft.module.customerTwo.companyManage.calculationExampleForm.service.ITable01474Service;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.Func;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2022-08-25
 */
@Service
@AllArgsConstructor
public class Table01474ServiceImpl extends BaseService<Table01474Mapper, Table01474> implements ITable01474Service {

	private ITable18607Service table18607Service;

	@Override
	public IPage<Table01474> getPageList(Table01474ListDto pageListDto) {
		Wrapper<Table01474> wrapper = Wrappers.<Table01474>query().lambda()
				.like(!StringUtil.isEmpty(pageListDto.getField100781c()), Table01474::getField100781c, pageListDto.getField100781c())
				.eq(!StringUtil.isEmpty(pageListDto.getField101800c()), Table01474::getField101800c, pageListDto.getField101800c())
				.eq(!StringUtil.isEmpty(pageListDto.getField107f8cb()), Table01474::getField107f8cb, pageListDto.getField107f8cb())
				.eq(!StringUtil.isEmpty(pageListDto.getField10260ca()), Table01474::getField10260ca, pageListDto.getField10260ca());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addTable01474(Table01474 table01474, List<Table18607> table18607List) {
		String table01474Id = IdWorker.get32UUID();
		table01474.setId(table01474Id);
		table18607List.forEach(table18607 -> table18607.setTable01474Id(table01474Id));
		table18607Service.saveBatch(table18607List);
		boolean isSuccess = this.save(table01474);
		return isSuccess ? table01474Id : null;
	}

	@Override
	public boolean updateTable01474(String id, Table01474 table01474, List<Table18607> table18607List) {
		table01474.setId(id);
		// Table18607
		List<String> table18607IdList = new ArrayList<>();
		for (Table18607 table18607 : table18607List) {
			String table18607Id = table18607.getId();
			if (StringUtil.isEmpty(table18607Id)) {
				table18607.setTable01474Id(id);
			} else {
				table18607IdList.add(table18607Id);
			}
		}
		if (CollectionUtil.isNotEmpty(table18607IdList)) {
			table18607Service.remove(Wrappers.<Table18607>query().lambda()
					.notIn(Table18607::getId, table18607IdList)
					.eq(Table18607::getTable01474Id, id));
		}
		table18607Service.saveOrUpdateBatch(table18607List);
		return this.updateById(table01474);
	}

	public boolean delete(String ids) {
		List<String> idList = Func.toStrList(ids);
		if (this.removeByIds(idList)) {
			table18607Service.remove(Wrappers.<Table18607>query().lambda().in(Table18607::getTable01474Id, ids));
			return true;
		}
		return false;
	}

	public List<Table18607> getTable18607ByParentId(String parentId){
		Wrapper wrapper = Wrappers.<Table18607>query().lambda().eq(Table18607::getTable01474Id, parentId);
		return table18607Service.list(wrapper);
	}
}